<template>
  <el-dialog :model-value="visible" title="加入疗程">
      <el-form>
          <el-form-item label="添加至疗程" :label-width="label_width">
              <el-select v-model="treatId" placeholder="选择疗程" size="default">
                  <el-option
                      v-for="item in treats"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"/>
              </el-select>
          </el-form-item>
          <el-form-item>
              <el-button
                  style="margin: auto"
                  type="primary"
                  @click="add2Treat">
                  添加
              </el-button>
          </el-form-item>
      </el-form>
  </el-dialog>
</template>

<script>

import axios from "axios";
import {ElMessage} from "element-plus";

export default {
    name: "TeethTreatPage",
    props: ['visible', 'id'],
    data() {
        return {
            label_width: '340px',
            teethId: 1,
            treatId: 0,
            treats: [
                {
                    value: 0,
                    label: '疗程1'
                },
                {
                    value: 1,
                    label: '疗程2'
                },
                {
                    value: 2,
                    label: '疗程3'
                }
            ]
        }
    },
    watch: {
        id: function (newData, oldData) {
            this.teethId = newData
        }
    },
    methods: {
        add2Treat: function () {
            if (this.treatId === 0) {
                this.addFailure("无效的疗程")
                return
            }

            let addForm = {
                teethId: this.teethId,
                treatId: this.treatId
            }
            const _this = this
            axios.post(process.env.VUE_APP_BASEURL + "treatment/adding", addForm).then(function (resp) {
                if (resp.data.code == 0) {
                    _this.addSuccess(resp.data.msg)
                } else {
                    _this.addFailure(resp.data.msg)
                }
            })
        },
        addSuccess: function (message) {
            this.$emit('add_success')
            ElMessage({
                message: message,
                type: 'success'
            })
        },
        addFailure: function (message) {
            ElMessage({
                message: message,
                type: 'error'
            })
        },
        getData: function () {
            const _this = this;
            axios.get(process.env.VUE_APP_BASEURL + "treatment/treatNamesByDentist?email=" + _this.$store.state.username).then(function (resp) {
                _this.treats = resp.data.data
            })
        }
    },
    mounted() {
        this.getData()
    }
}

</script>

<style scoped>

</style>